package com.cloudfast.controller;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cloudfast.common.jdbc.reflect.ReflectUtils;
import com.cloudfast.common.page.Page;
import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.context.error.ErrorCode;
import com.cloudfast.dao.SimpleRepository;
import com.cloudfast.dao.conf.EntityParseInfo;
import com.cloudfast.entity.BaseEntity;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 基础控制层实现快速CURD
 *
 * @param <T>
 * @author liuyw
 * @date 2022年9月7日
 */
@ApiIgnore
public class BaseController<T extends BaseEntity> {
    protected static final Logger logger = LoggerFactory.getLogger(Logger.class);

    private EntityParseInfo einfo;

    @Autowired
    private SimpleRepository repository;

    @SuppressWarnings("unchecked")
    public BaseController() {
        Type genType = getClass().getGenericSuperclass();
        if (genType instanceof ParameterizedType) {
            Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
            Class<T> mEntityClass = (Class<T>) params[0];
            einfo = ReflectUtils.parseEntityInfo(mEntityClass);
        }
    }

    /**
     * @说明:分页
     * @开发: liuyw
     * @时间: 2019年2月26日
     * @参数:@param pageSize
     * @参数:@param pageNumber
     * @参数:@return
     */
    @PostMapping(value = "findPage")
    @ApiOperation("分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNo", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页值", required = true)})
    public ApiResponse findPage(@ApiIgnore Page<T> page, @ApiIgnore T entity) {
        try {
            return ApiResponse.success(repository.nativeQueryPage(page, entity, einfo));

        } catch (Exception e) {
            return ApiResponse.message(ErrorCode.FIND_PAGE_ERROR);
        }
    }

    /**
     * @param entity
     * @return
     * @描述:保存实体类
     * @author liuyw
     * @date 2020年4月12日
     */
    @PostMapping(value = "save")
    @ApiOperation("保存")
    public ApiResponse save(T entity) {
        try {
            repository.saveOrUpdate(entity, einfo);
            return ApiResponse.success(true);
        } catch (Exception e) {
            return ApiResponse.message(ErrorCode.SAVE_ERROR);
        }
    }

    /**
     * @param id
     * @return
     * @描述:删除实体类
     * @author liuyw
     * @date 2020年4月12日
     */
    @PostMapping(value = "delete")
    @ApiOperation("删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", required = true)})
    public ApiResponse delete(Long id) {
        try {
            repository.delete(id, einfo);
            return ApiResponse.success(true);
        } catch (Exception e) {
            return ApiResponse.message(ErrorCode.DELETE_ERROR);
        }
    }

    /**
     * @param id 实体类中的主键
     * @return
     * @描述:根据查询实体类
     * @author liuyw
     * @date 2020年4月12日
     */
    @PostMapping(value = {"findOne"})
    @ApiOperation("根据主键查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", required = true)})
    public ApiResponse findOne(@RequestParam Long id) {
        try {
            T entity = repository.findOne(id, einfo);
            return ApiResponse.success(entity);
        } catch (Exception e) {
            return ApiResponse.message(ErrorCode.FIND_ONE_ERROR);
        }
    }

    @PostMapping(value = {"findList"})
    @ApiOperation("根据条件查询")
    public ApiResponse findList(T entity) {
        try {
            return ApiResponse.success(repository.nativeQueryList(entity, einfo));
        } catch (Exception e) {
            return ApiResponse.message(ErrorCode.SYSTEM_ERROR);
        }
    }
}
